package com.lxl.securitypermission.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author 绫小路
 * @date 2021/1/6 19:36
 * @description
 */
@Configuration
public class SwaggerConfig {

  @Value("${spring.profiles.active}")
  private String active;

  /**
   * 创建API应用 apiInfo() 增加API相关信息 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现， 本例采用指定扫描的包路径来定义指定要建立API的目录。
   *
   * @return
   */
  @Bean
  public Docket createRestApi() {
    //判断是否开启api
    boolean open = "dev".equals(active);
    return new Docket(DocumentationType.SWAGGER_2)
        .enable(open)
        .apiInfo(apiInfo())
        .select()
        //扫描指定包
        .apis(RequestHandlerSelectors.basePackage("com.lxl.securitypermission.controller"))
//        .apis(RequestHandlerSelectors.basePackage("com.example.flowabledemo.controller"))
        .paths(PathSelectors.any())
        .build();
  }

  /**
   * 创建该API的基本信息（这些基本信息会展现在文档页面中） http://localhost:8080/swagger-ui/index.html#/
   *
   * @return
   */
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("security-permission 文档")
        .description("这是spring security演示项目")
        .termsOfServiceUrl("http://localhost:8080")
        .version("1.0.1")
        .build();
  }
}
